#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
/*
 * Copyright © 2014, Finium Sdn Bhd, All Rights Reserved
 * 
 * BaseMvcTestCase.java
 * Modification History
 * *************************************************************
 * Date				Author						Comment
 * Jun 11, 2012		Venkaiah Chowdary Koneru	Created
 * *************************************************************
 */
package ${package}.test;

import java.io.IOException;
import java.sql.SQLException;

import javax.annotation.Resource;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.icegreen.greenmail.util.GreenMail;
import com.icegreen.greenmail.util.ServerSetupTest;

/**
 * Base class for unit testing spring MVC annotated controllers with spring
 * context.
 * 
 * @author Venkaiah Chowdary Koneru
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext-test.xml",
        "classpath:dispatcher-test-servlet.xml" })
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
@WebAppConfiguration
public class BaseMvcTestCase
{
    private static MockMvc mockMvc;

    @Resource
    private WebApplicationContext webApplicationContext;
    
    private static GreenMail greenMail;
    
    /**
     * Initial setup of the test case. initialises all the beans defined in
     * application context and dispatcher-servlet context.
     * 
     * @throws IOException
     * @throws SQLException
     */
    @BeforeClass
    public static void setupContext() throws SQLException, IOException
    {
        UtilTestCase.setupContext();
        
        greenMail = new GreenMail(ServerSetupTest.SMTP);
        greenMail.start();
    }

    /**
     * to setup web context
     */
    public void setUpWebContext()
    {
    	if(mockMvc == null)
    	{
	    	mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
	                .build();
    	}
    }
    
    /**
     * to be run after every Test method
     * 
     * @throws SQLException
     * @throws IOException
     */
    @AfterClass
    public static void afterTestMethod() throws SQLException, IOException
    {
        UtilTestCase.afterTestMethod();
        
        greenMail.stop();
    }

    /**
     * returns mock MVC object
     * 
     * @return Mock MVC instance
     */
    protected MockMvc getMockMvc()
    {
        return mockMvc;
    }
}
